home *** CD-ROM | disk | FTP | other *** search
/ Power Bytes: Money & Finance / PowerBytes Money and Finance CD-ROM 01 / PowerBytes Money and Finance CD-ROM 01.iso / HyperCard Files / TEXAS / C progs / qndxr.2 ƒ / build_indices.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-03  |  3.6 KB  |  114 lines  |  [TEXT/KAHL]

  1. /* file build_indices.c ... by ^z, 870820-0913-...
  2.  *
  3.  * revised 870930-871007 to allow more user options on keeping/discarding
  4.  *    punctuation, etc. -- ideas based on Bill Hole's suggestions
  5.  *
  6.  * contains subroutine to build indices for each chunk of input document
  7.  * (database) text file for program qndxr ... 
  8.  *
  9.  * Strategy is as outlined in qndxr:  take in a big chunk of the doc_file,
  10.  *    generate the pointers to each word in the buffer as the buffer contents
  11.  *    are converted into appropriate (all caps, delimiters filtered into
  12.  *    spaces) form for sorting; sort the pointers in memory; and then write
  13.  *    out to disk the pointers and keys in the ndxr.c format for *.k and *.p
  14.  *    files.
  15.  *
  16.  * Allocate space for the doc and ptr buffers here so as to maximize use
  17.  * of available memory ... note that we need to have room for the doc
  18.  * buffer, for a ptr buffer that might (in the worst case of a file full
  19.  * of 1-letter words) be twice as long as the doc buffer, and also space
  20.  * for two standard zbuffers to accumulate the output *.k and *.p file
  21.  * info in before sending it to disk.
  22.  *
  23.  * Note that for speed, while they are being sorted the pointers just point
  24.  *    directly to the key strings in the input buffer; they must be converted
  25.  *    into true offset pointers relative to the 0th byte of the document file
  26.  *    as they are written to disk in the *.p file!  Make sure that all of
  27.  *    the delimiters in the document/database buffer are converted into
  28.  *    '\0' characters so that string comparison functions will work right.
  29.  *
  30.  * Also note that to avoid edge effects at the end of the buffer, an extra
  31.  *    amount of space is required at the end of the buffer, of length
  32.  *    KEY_LENGTH, to accomodate the end of the last word in the buffer.
  33.  *
  34.  * Use static local variables in the function here to keep track of where
  35.  *    we are in the document file from one chunk to the next, what chunk
  36.  *    number we are on now, etc.
  37.  *
  38.  * Give the user a chance to interrupt operations (in the Macintosh
  39.  * version of this program) at intervals here, as long as
  40.  * there are time-consuming I/O or sorting or scanning operations
  41.  * to be done ...
  42.  */
  43.  
  44. #include <stdio.h>
  45. #include <unix.h>
  46. #include <storage.h>
  47. #include <strings.h>
  48. #include <ctype.h>
  49. #include <proto.h>
  50. #include "qndxr.2.h"
  51.  
  52. int build_indices ()
  53.   {
  54.     static int pass_number = 0;
  55.     long doc_bufsiz, offset, load_doc_buffer(), nwords, 
  56.             ftell();
  57.     extern long zbufsiz;
  58.     extern FILE *doc_file;
  59.     char *doc, **ptr, *malloc(), *mlalloc(), *calloc(), *clalloc();
  60.     void zqsort(), write_sorted_files();
  61.  
  62.     doc_bufsiz = 2 * NMERGE * zbufsiz / 3;
  63.     DEBUG ("--allocating doc buffer of size %ld\n", doc_bufsiz + KEY_LENGTH);
  64.     doc = make_buf (doc_bufsiz + KEY_LENGTH);
  65.     
  66.     DEBUG ("--allocating ptr buffer of size %ld\n", doc_bufsiz * 2);
  67.     ptr = (char **)make_buf (doc_bufsiz * 2);
  68.     
  69. #ifdef LIGHTSPEED
  70.     check_interrupt ();
  71. #endif
  72.  
  73.     offset = ftell (doc_file);    
  74.     DEBUG ("--loading doc buffer beginning at offset %ld\n", offset);    
  75.     nwords = load_doc_buffer (doc, doc_bufsiz, ptr);
  76.  
  77.     if (nwords == 0)
  78.       {
  79.         DEBUG ("--Building done ... now freeing doc & ptr buffers\n", NULL);
  80.         free (doc);
  81.         free ((char *)ptr);
  82.         return (FALSE);
  83.       }
  84.     
  85.     printf ("Index subfile #%d contains %ld words...\n", pass_number,
  86.                 nwords);
  87.     
  88. #ifdef LIGHTSPEED
  89.     check_interrupt ();
  90. #endif
  91.  
  92.     DEBUG ("--sorting ptr array\n", NULL);
  93.     zqsort (ptr, ptr + nwords);
  94.     
  95. #ifdef LIGHTSPEED
  96.     check_interrupt ();
  97. #endif
  98.  
  99.     DEBUG ("--writing sorted keys and ptrs to disk\n", NULL);
  100.     write_sorted_files (doc, ptr, nwords, pass_number, offset);
  101.     
  102. #ifdef LIGHTSPEED
  103.     check_interrupt ();
  104. #endif
  105.  
  106.     DEBUG ("--freeing doc & ptr buffers\n", NULL);
  107.     free (doc);
  108.     free ((char *)ptr);
  109.     
  110.     ++pass_number;
  111.     return (TRUE);
  112.   }
  113.  
  114.